import {
	login,
	tempLogin,
	logout,
	getUserInfo,
	changePwd,
	changeInfo,
	register,
	renewal,
	verifyCode,
	findPass,
	sessionKey,
	getUserNumber,
	getHeartBeat
} from '@/api/user'
import {fetchLogin} from '@/api/scan.js'
import rsa from '@/utils/rsa.js'

import {
	setToken,
	removeToken
} from '@/utils/auth'

const state = {
	 
	token: {
		code: '',
		expTime: ''
	},
	//临时token
	tempToken: {
		code: '',
		expTime: ''
	},
	info: {},
}

const mutations = {
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_TEMP_TOKEN: (state, token) => {
		state.tempToken = token
	},
	SET_INFO: (state, info) => {
		state.info = info
	},
	SET_TOKEN_NO(state, args) {
		state.token = {}
	},
	SET_TEMP_TOKEN_NO(state, args) {
		state.tempToken = {}
	}
}

const actions = {
	// 登录
	login({
		state,
		commit
	}, userInfo) {  
		return new Promise((resolve, rej) => { 
			 
			const params = {
				...userInfo,
				username: userInfo.username.trim(),
				password: rsa.encryptLong(userInfo.password.trim())
			}
			fetchLogin(params).then(res =>{
				if(res.token){ 
					commit('SET_TOKEN_NO',res.token)
					setToken(res.token)
					resolve()
				}else{
					rej()
				}
			   
			}).catch(()=>{
				rej()
			})
		})
	},
  
	// 获取用户信息
	getUserInfo({
		commit,
		dispatch
	}, args) {
		if (typeof args == 'undefined' || args !== false) {
			args = true
		}
		let cache_role = uni.getStorageSync('cache_role')
		dispatch('cacheRole', cache_role)
		return new Promise((resolve, reject) => {
			getUserInfo().then(res => {
				//判断角色是否缓存
				if (res.data.roleCodes != cache_role) {
					this.dispatch('configs/setTabbars', {
						codes: res.data.roleCodes,
						flag: args
					})
					//添加角色缓存
					uni.setStorageSync('cache_role', res.data.roleCodes)
				}

				commit('SET_INFO', res.data || {})

				return resolve(res)
			}).catch(error => {
				return reject(error)
			})
		})
	},


	// 登出
	logout({
		commit,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			// 	logout().then(() => {
			// commit('SET_TOKEN', '')
			commit('SET_INFO', {})
			let user = uni.getStorageSync('user')
			if (user) {
				uni.setStorageSync("user", {
					username: user.username,
					password: "",
				})
			}
			// removeToken()
			//删除角色缓存
			// uni.removeStorageSync('cache_role')
			//重置tabar
			// dispatch('configs/setTabbars', {}, { root: true })
			//清空数据  
			return resolve()
		})
	},
	// 修改密码
	changePwd({
		commit
	}, args) {
		return new Promise((resolve, reject) => {
			changePwd(args).then(res => {
				// this.dispatch('user/getUserInfo')
				return resolve(res || {})
			}).catch(error => {
				return reject(error)
			})
		})
	},
	// 找回密码
	findPass({
		commit
	}, args) {
		return new Promise((resolve, reject) => {
			findPass({
				body: args
			}).then(res => {
				return resolve(res || {})
			}).catch(error => {
				return reject(error)
			})
		})
	},

	// 用户注册
	register({
		commit
	}, args) {
		return new Promise((resolve, reject) => {
			register(args).then(res => {
				return resolve(res || {})
			}).catch(error => {
				return reject(error)
			})
		})
	},
	//获取微信登陆sessionKey
	getSessionKey(store, args) {
		return new Promise((resolve, reject) => {
			sessionKey(args).then(res => {
				return resolve(res.data || {})
			}).catch(error => {
				return reject(error)
			})
		})
	},
	//获取电话号码
	getUserNumber(store, args) {
		return new Promise((resolve, reject) => {
			getUserNumber(args).then(res => {
				return resolve(res.data || {})
			}).catch(error => {
				return reject(error)
			})
		})
	},

	setToken({
		commit
	}, args) {
		commit('SET_TOKEN_NO', args)
	},
	renewalToken({
		commit
	}, args) {
		return new Promise(resolve => {
			renewal({}).then((res) => {
				let data = res.data
				commit('SET_TOKEN', data.token)
				setToken(data.token)
				resolve(data)
			}).catch(_ => {

			})
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}